package com.chuanyang.driverh.mainmy;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.TextView;

import com.chuanyang.driverh.BaseApplication;
import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseTitleActivity;
import com.chuanyang.driverh.mainmy.adapter.CommonLineAdapter;
import com.chuanyang.driverh.mainother.LookPhotoDetailActivity;
import com.chuanyang.driverh.responseresult.DriverInfoData;
import com.chuanyang.driverh.responseresult.DriverInfoResult;
import com.chuanyang.driverh.responseresult.LinesManageData;
import com.chuanyang.driverh.responseresult.LinesManageResult;
import com.chuanyang.driverh.utils.ActivityUtil;
import com.chuanyang.driverh.utils.BitmapUtil;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.SharedPrefUtil;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;
import com.chuanyang.driverh.utils.widget.CustomMeasureHeightListView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;

/**
 * Created by xupeixian on 2017/2/20.
 */

public class MyCarHadAccreditationActivity extends BaseTitleActivity {
    private DriverInfoData driverinfo;
    private TextView tv_mycar_captity;
    private TextView tv_mycar_realcaptity;
    private TextView tv_mycar_cartype;
    private TextView tv_mycar_carstandard;
    private TextView tv_mycar_carcode;
    private TextView tv_mycar_transportcard;
    private RatingBar rb_mycar_star;
    private TextView tv_mycar_rating;
    private ImageView iv_driver_details_car_img1;
    private ImageView iv_driver_details_car_img2;
    private ImageView iv_driver_details_car_img3;
    private LinearLayout ll_mycarhadaccreditation_offenlinestitle;
    private CustomMeasureHeightListView listView;
    private LinearLayout ll_offenlinestitle;
    private ImageView iv_mycar_trasportstatus;
    private TextView tv_carship_accr;
    //获取传递来的数据，"1"表示车辆已认证，从个人中心进入   "2"表示车辆信息，点击我的车队成员的图像进入
    // "1"包含常用路线，"2"没有常用路线
    private String index;
    private boolean isCarAccreditation;
    @Override
    protected int getContentResId() {
        return R.layout.my_act_mycarhadaccreditation;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tv_mycar_captity = (TextView) findViewById(R.id.tv_mycar_captity);
        tv_mycar_realcaptity = (TextView) findViewById(R.id.tv_mycar_realcaptity);
        tv_mycar_cartype = (TextView) findViewById(R.id.tv_mycar_cartype);
        tv_mycar_carstandard = (TextView) findViewById(R.id.tv_mycar_carstandard);
        //车牌号
        tv_mycar_carcode = (TextView) findViewById(R.id.tv_mycar_carcode);
        iv_mycar_trasportstatus = (ImageView) findViewById(R.id.iv_mycar_trasportstatus);
        //驾驶证
        tv_mycar_transportcard = (TextView) findViewById(R.id.tv_mycar_transportcard);
        rb_mycar_star = (RatingBar) findViewById(R.id.tv_mycar_star);
        tv_mycar_rating = (TextView) findViewById(R.id.tv_mycar_rating);
        iv_driver_details_car_img1 = (ImageView) findViewById(R.id.iv_driver_details_car_img1);
        iv_driver_details_car_img2 = (ImageView) findViewById(R.id.iv_driver_details_car_img2);
        iv_driver_details_car_img3 = (ImageView) findViewById(R.id.iv_driver_details_car_img3);
        listView = (CustomMeasureHeightListView) findViewById(R.id.ll_mycarhadaccreditation_offenlines);
        //常运路线
        ll_offenlinestitle = (LinearLayout) findViewById(R.id.ll_mycarhadaccreditation_offenlinestitle);
    }

    @Override
    protected void initData() {
        setBackImg();
        driverinfo = BaseApplication.getInstance().getDriverInfo();//司机信息的常用路线只有条；
        index = getIntent().getStringExtra("data");
        updateUi();
        if (index.equals("1")) {
            isCarAccreditation = true;
            getTitleTextView().setText("我的车辆");
            getRightTextView().setText("重新认证");
            getLines(driverinfo.getDriverId() + "");//获取常运路线
        }
        else if (index.equals("2")) {
            //请求车队成员的车辆信息，获取车辆信息，获取常运路线信息
            isCarAccreditation = false;
            getTitleTextView().setText("车辆信息");
            ll_offenlinestitle.setVisibility(View.GONE);
            listView.setVisibility(View.GONE);
            if (TextUtils.isEmpty(getIntent().getStringExtra("menmberId"))) {
                //查询司机信息
                getDriverInfo(getIntent().getStringExtra("menmberId"));
            }
        }



    }

    private void updateUi() {
        if (driverinfo != null) {
            if (driverinfo.getDriver().getTransportState() == null) {
                iv_mycar_trasportstatus.setImageResource(R.mipmap.free);
            }
            else {
                if (driverinfo.getDriver().getTransportState() == 1) {
                    iv_mycar_trasportstatus.setImageResource(R.mipmap.transport);
                } else {
                    iv_mycar_trasportstatus.setImageResource(R.mipmap.free);
                }
            }
            rb_mycar_star.setRating(driverinfo.getDriver().getDriverRank());
            tv_mycar_cartype.setText(driverinfo.getDriver().getCarType());
            tv_mycar_carstandard.setText(driverinfo.getDriver().getCarStandard());
            tv_mycar_captity.setText(driverinfo.getDriver().getCapacity() + "t");
            tv_mycar_realcaptity.setText(driverinfo.getDriver().getActualCapacity() + "");
            tv_mycar_carcode.setText(driverinfo.getDriver().getPlateNumber());
            tv_mycar_transportcard.setText(driverinfo.getDriver().getDriverInsurance());//此处填保险单号


            if(!TextUtils.isEmpty(driverinfo.getDriver().getCarPt1())){
                BitmapUtil.showImgForNetPath(driverinfo.getDriver().getCarPt1(),iv_driver_details_car_img1);
                iv_driver_details_car_img1.setVisibility(View.VISIBLE);
            } else {
                iv_driver_details_car_img1.setVisibility(View.GONE);
            }
            if(!TextUtils.isEmpty(driverinfo.getDriver().getCarPt2())){
                BitmapUtil.showImgForNetPath(driverinfo.getDriver().getCarPt2(), iv_driver_details_car_img2);
                iv_driver_details_car_img2.setVisibility(View.VISIBLE);
            } else {
                iv_driver_details_car_img2.setVisibility(View.GONE);
            }
            if(!TextUtils.isEmpty(driverinfo.getDriver().getCarPt3())){
                BitmapUtil.showImgForNetPath(driverinfo.getDriver().getCarPt3(), iv_driver_details_car_img3);
                iv_driver_details_car_img3.setVisibility(View.VISIBLE);
            } else {
                iv_driver_details_car_img3.setVisibility(View.GONE);
            }


        }


    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId())
        {
            case  R.id.iv_driver_details_car_img1:
                ActivityUtil.startActivity(activityContext,new Intent(activityContext,LookPhotoDetailActivity.class).putExtra("data", driverinfo.getDriver().getCarPt1()));
                break;
            case  R.id.iv_driver_details_car_img2:
                ActivityUtil.startActivity(activityContext,new Intent(activityContext,LookPhotoDetailActivity.class).putExtra("data",driverinfo.getDriver().getCarPt2()));
                break;
            case  R.id.iv_driver_details_car_img3:
                ActivityUtil.startActivity(activityContext,new Intent(activityContext,LookPhotoDetailActivity.class).putExtra("data", driverinfo.getDriver().getCarPt3()));
                break;
        }
    }

    @Override
    protected void initListener() {
        iv_driver_details_car_img1.setOnClickListener(this);
        iv_driver_details_car_img2.setOnClickListener(this);
        iv_driver_details_car_img3.setOnClickListener(this);

    }

    @Override
    protected void baseGoEnsure() {
        ActivityUtil.startActivity(activityContext,AccredCarOrShipTeamActivity.class);
        finish();
    }



    /**
     * 获取司机的常用路线
     */
    private void getLines(String driverId) {
        DialogUtil.showLoadingDialog(activityContext,"路线...");
        Map<String,String> params=new HashMap<String, String>();
        params.put("driverId", SharedPrefUtil.getUserId());

        MyHttpUtil.getLines(activityContext,params, new MyCallback<LinesManageResult>(activityContext,new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(activityContext, "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(LinesManageResult result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    //登录成功返回用户信息
                    if (result.getData() != null) {
                        fillCommonLine(result.getData().getData());
                    }
                }else{
                    PrintUtil.toast(activityContext,result.getMsg());
                }
            }
        });
    }

    private void fillCommonLine(List<LinesManageData.DataBean> list) {
        listView.setAdapter(new CommonLineAdapter(MyCarHadAccreditationActivity.this, list));

    }

    /**
     * 获取司机信息
     */
    public void getDriverInfo(String driverId) {//有个人信息修改的情况，重新从网络获取个人信息。
        DialogUtil.showLoadingDialog(activityContext,"");
        Map<String,String> params=new HashMap<String, String>();
        params.put("driverId", driverId);
        MyHttpUtil.selectUser(activityContext,params, new MyCallback<DriverInfoResult>(activityContext,new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(activityContext, "网络异常，请稍后再试");
            }

            @Override
            public void onSuccess(DriverInfoResult result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    driverinfo = result.getData();
                    updateUi();

                }else{
                    PrintUtil.toast(activityContext,result.getMsg());
                }
            }
        });
    }



}
